<div class="usercenter">
<include file="User/_menu" />
	<div class="content">
		<div class="title">我的购物车</div>
		<form method="post" action="{:U('Order/buy')}">
			<table class="shopcart">
				<tr><th width="60"><a href="#" class="checkAll">全选</a></th>
				<th>商品</th><th width="150">单价(元)</th><th width="95">数量</th><th width="120">操作</th></tr>
				<foreach name="cart" item="v" key="k">
					<tr class="item">
					<td class="center"><input type="checkbox" name="buy[{$k}][id]" class="check" value="{$v.goods_id}" /></td>
					<td><a href="/?a=goods&id={$v.goods_id}" target="_blank" class="bold">{$v.name}</a></td>
					<td class="center"><span class="item-price">{$v.price}</span></td>
					<td class="center">
						<button class="setNum">-</button>
						<input name="buy[{$k}][num]" class="item-num" type="text" value="{$v.num}" />
						<button class="setNum">+</button>
					</td>
					<td class="center"><a href="{:U('Cart/del',array('id'=>$v['id']))}" />删除</a></td>
					</tr>
				</foreach>
				<tr class="act"><td class="center"><a href="#" class="checkAll">全选</a></td>
				<td colspan="4">
					共<span id="num"></span>件商品 总计：<span class="price">￥<span id="monery"></span></span>
					<input type="submit" value="提交订单" class="order-btn" />
				</td></tr>
			</table>
		</form>
	</div>
</div>
<script>
(function(){
	var $check = $(".check");  //获取复选框对象
	var isCheckAll = false;    //当前是否全选
	$check.click(total);	   //单击复选框时更新总价格
	checkAll();				   //默认设为全选
	//全选、全不选
	$(".checkAll").click(checkAll);
	function checkAll(){
		isCheckAll = isCheckAll ? false : true;
		$check.prop("checked", isCheckAll);
		$check.attr("checked", isCheckAll);
		total();  //更新价格
	}
	//单击修改数量
	$(".setNum").click(function(){
		var $item = $(this).parent().find(".item-num");
		var act = $(this).text();
		var num = parseInt($item.val());
		var stock = 100;
		if (act === '-') {
			if (num === 1) return false;
			$item.val(num-1);
		}else if (act === '+') {
			if (num === stock) return false;
			$item.val(num+1);
		}
		total();
		return false;
	});
	//键盘修改数量
	$(".item-num").keyup(function(){
		var num = parseInt($(this).val());
		var stock = 100;
		if(num < 1){
			$(this).val(1);
		}else if(num > stock){
			$(this).val(stock);
		}
		total();
	});
	//计算总价
	function total() {
		var count = 0; //总数量
		var total = 0; //总价格
		//遍历勾选的商品
		$(".check:checked").each(function(){
			var $item = $(this).parents(".item");
			var price = parseFloat($item.find(".item-price").text());
			var num = parseInt($item.find(".item-num").val());
			count += num;
			total += price * num;
		});
		$("#monery").text(total.toFixed(2));
		$("#num").text(count);
	}
})();
</script>